Passed
Branch master (0c09a6)
by Stephan
01:45
created

tab.js ➔ ... ➔ _createClass   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 4
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
/*!
2
  * Bootstrap tab.js v4.3.1 (https://getbootstrap.com/)
3
  * Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
5
  */
6 View Code Duplication
(function (global, factory) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = global || self, global.Tab = factory(global.jQuery, global.Util));
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
10
}(this, function ($, Util) { 'use strict';
11
12
  $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
13
  Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;
14
15
  function _defineProperties(target, props) {
16
    for (var i = 0; i < props.length; i++) {
17
      var descriptor = props[i];
18
      descriptor.enumerable = descriptor.enumerable || false;
19
      descriptor.configurable = true;
20
      if ("value" in descriptor) descriptor.writable = true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
21
      Object.defineProperty(target, descriptor.key, descriptor);
22
    }
23
  }
24
25
  function _createClass(Constructor, protoProps, staticProps) {
26
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
27
    if (staticProps) _defineProperties(Constructor, staticProps);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
28
    return Constructor;
29
  }
30
31
  /**
32
   * ------------------------------------------------------------------------
33
   * Constants
34
   * ------------------------------------------------------------------------
35
   */
36
37
  var NAME = 'tab';
38
  var VERSION = '4.3.1';
39
  var DATA_KEY = 'bs.tab';
40
  var EVENT_KEY = "." + DATA_KEY;
41
  var DATA_API_KEY = '.data-api';
42
  var JQUERY_NO_CONFLICT = $.fn[NAME];
43
  var Event = {
44
    HIDE: "hide" + EVENT_KEY,
45
    HIDDEN: "hidden" + EVENT_KEY,
46
    SHOW: "show" + EVENT_KEY,
47
    SHOWN: "shown" + EVENT_KEY,
48
    CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
49
  };
50
  var ClassName = {
51
    DROPDOWN_MENU: 'dropdown-menu',
52
    ACTIVE: 'active',
53
    DISABLED: 'disabled',
54
    FADE: 'fade',
55
    SHOW: 'show'
56
  };
57
  var Selector = {
58
    DROPDOWN: '.dropdown',
59
    NAV_LIST_GROUP: '.nav, .list-group',
60
    ACTIVE: '.active',
61
    ACTIVE_UL: '> li > .active',
62
    DATA_TOGGLE: '[data-toggle="tab"], [data-toggle="pill"], [data-toggle="list"]',
63
    DROPDOWN_TOGGLE: '.dropdown-toggle',
64
    DROPDOWN_ACTIVE_CHILD: '> .dropdown-menu .active'
65
    /**
66
     * ------------------------------------------------------------------------
67
     * Class Definition
68
     * ------------------------------------------------------------------------
69
     */
70
71
  };
72
73
  var Tab =
74
  /*#__PURE__*/
75
  function () {
76
    function Tab(element) {
77
      this._element = element;
78
    } // Getters
79
80
81
    var _proto = Tab.prototype;
82
83
    // Public
84
    _proto.show = function show() {
85
      var _this = this;
86
87
      if (this._element.parentNode && this._element.parentNode.nodeType === Node.ELEMENT_NODE && $(this._element).hasClass(ClassName.ACTIVE) || $(this._element).hasClass(ClassName.DISABLED)) {
0 ignored issues
show
Bug introduced by
The variable Node seems to be never declared. If this is a global, consider adding a /** global: Node */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
88
        return;
89
      }
90
91
      var target;
92
      var previous;
93
      var listElement = $(this._element).closest(Selector.NAV_LIST_GROUP)[0];
94
      var selector = Util.getSelectorFromElement(this._element);
95
96
      if (listElement) {
97
        var itemSelector = listElement.nodeName === 'UL' || listElement.nodeName === 'OL' ? Selector.ACTIVE_UL : Selector.ACTIVE;
98
        previous = $.makeArray($(listElement).find(itemSelector));
99
        previous = previous[previous.length - 1];
100
      }
101
102
      var hideEvent = $.Event(Event.HIDE, {
103
        relatedTarget: this._element
104
      });
105
      var showEvent = $.Event(Event.SHOW, {
106
        relatedTarget: previous
0 ignored issues
show
Bug introduced by
The variable previous does not seem to be initialized in case listElement on line 96 is false. Are you sure this can never be the case?
Loading history...
107
      });
108
109
      if (previous) {
110
        $(previous).trigger(hideEvent);
111
      }
112
113
      $(this._element).trigger(showEvent);
114
115
      if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) {
116
        return;
117
      }
118
119
      if (selector) {
120
        target = document.querySelector(selector);
121
      }
122
123
      this._activate(this._element, listElement);
124
125
      var complete = function complete() {
126
        var hiddenEvent = $.Event(Event.HIDDEN, {
127
          relatedTarget: _this._element
128
        });
129
        var shownEvent = $.Event(Event.SHOWN, {
130
          relatedTarget: previous
0 ignored issues
show
Bug introduced by
The variable previous does not seem to be initialized in case listElement on line 96 is false. Are you sure this can never be the case?
Loading history...
131
        });
132
        $(previous).trigger(hiddenEvent);
133
        $(_this._element).trigger(shownEvent);
134
      };
135
136
      if (target) {
137
        this._activate(target, target.parentNode, complete);
138
      } else {
139
        complete();
140
      }
141
    };
142
143
    _proto.dispose = function dispose() {
144
      $.removeData(this._element, DATA_KEY);
145
      this._element = null;
146
    } // Private
147
    ;
148
149
    _proto._activate = function _activate(element, container, callback) {
150
      var _this2 = this;
151
152
      var activeElements = container && (container.nodeName === 'UL' || container.nodeName === 'OL') ? $(container).find(Selector.ACTIVE_UL) : $(container).children(Selector.ACTIVE);
153
      var active = activeElements[0];
154
      var isTransitioning = callback && active && $(active).hasClass(ClassName.FADE);
155
156
      var complete = function complete() {
157
        return _this2._transitionComplete(element, active, callback);
158
      };
159
160
      if (active && isTransitioning) {
161
        var transitionDuration = Util.getTransitionDurationFromElement(active);
162
        $(active).removeClass(ClassName.SHOW).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
163
      } else {
164
        complete();
165
      }
166
    };
167
168
    _proto._transitionComplete = function _transitionComplete(element, active, callback) {
169
      if (active) {
170
        $(active).removeClass(ClassName.ACTIVE);
171
        var dropdownChild = $(active.parentNode).find(Selector.DROPDOWN_ACTIVE_CHILD)[0];
172
173
        if (dropdownChild) {
174
          $(dropdownChild).removeClass(ClassName.ACTIVE);
175
        }
176
177
        if (active.getAttribute('role') === 'tab') {
178
          active.setAttribute('aria-selected', false);
179
        }
180
      }
181
182
      $(element).addClass(ClassName.ACTIVE);
183
184
      if (element.getAttribute('role') === 'tab') {
185
        element.setAttribute('aria-selected', true);
186
      }
187
188
      Util.reflow(element);
189
190
      if (element.classList.contains(ClassName.FADE)) {
191
        element.classList.add(ClassName.SHOW);
192
      }
193
194
      if (element.parentNode && $(element.parentNode).hasClass(ClassName.DROPDOWN_MENU)) {
195
        var dropdownElement = $(element).closest(Selector.DROPDOWN)[0];
196
197
        if (dropdownElement) {
198
          var dropdownToggleList = [].slice.call(dropdownElement.querySelectorAll(Selector.DROPDOWN_TOGGLE));
199
          $(dropdownToggleList).addClass(ClassName.ACTIVE);
200
        }
201
202
        element.setAttribute('aria-expanded', true);
203
      }
204
205
      if (callback) {
206
        callback();
207
      }
208
    } // Static
209
    ;
210
211
    Tab._jQueryInterface = function _jQueryInterface(config) {
212
      return this.each(function () {
213
        var $this = $(this);
214
        var data = $this.data(DATA_KEY);
215
216
        if (!data) {
217
          data = new Tab(this);
218
          $this.data(DATA_KEY, data);
219
        }
220
221
        if (typeof config === 'string') {
222
          if (typeof data[config] === 'undefined') {
223
            throw new TypeError("No method named \"" + config + "\"");
224
          }
225
226
          data[config]();
227
        }
228
      });
229
    };
230
231
    _createClass(Tab, null, [{
232
      key: "VERSION",
233
      get: function get() {
234
        return VERSION;
235
      }
236
    }]);
237
238
    return Tab;
239
  }();
240
  /**
241
   * ------------------------------------------------------------------------
242
   * Data Api implementation
243
   * ------------------------------------------------------------------------
244
   */
245
246
247
  $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
248
    event.preventDefault();
249
250
    Tab._jQueryInterface.call($(this), 'show');
251
  });
252
  /**
253
   * ------------------------------------------------------------------------
254
   * jQuery
255
   * ------------------------------------------------------------------------
256
   */
257
258
  $.fn[NAME] = Tab._jQueryInterface;
259
  $.fn[NAME].Constructor = Tab;
260
261
  $.fn[NAME].noConflict = function () {
262
    $.fn[NAME] = JQUERY_NO_CONFLICT;
263
    return Tab._jQueryInterface;
264
  };
265
266
  return Tab;
267
268
}));
269
//# sourceMappingURL=tab.js.map
270